<?php
/**
 * Created by PhpStorm.
 * User:雨寒
 * Date: 2018/9/25
 * Time: 15:39
 */

namespace app\www\controller;

use think\Session;
use think\Db;
use app\common\model\User;
use app\common\model\DeveloperException;
class Connection extends Common
{

    //wap Index/index 首页
    public function index(){
        $access_token=Session::get("access_token");
        $data=array(
            "module"=>'Connection',
            "interface"=>'1001',
            "version"=>'1.0.0',
            "data"=>array(
                "access_token"=>$access_token
            )
        );
        $res=curl_post(config('self_url'),json_encode($data));
//var_dump(json_decode($res)->result_data);die;
        $result_code=json_decode($res,true)['result_code'];
        $result_info=json_decode($res,true)['result_info'];
        if($result_code!=="0000")
        {
            if($result_code=="1111")
            {
                $this->redirect('Login/login');
            }
            $this->error($result_info);
        }
        $result_data=json_decode($res,true)['result_data'];
        $this->assign("member_num",$result_data['member_num']);
        $this->assign("news_list",$result_data['news_list']);
        $this->assign("banner_list",$result_data['banner_list']);
        $this->assign("bussiness_list",$result_data['bussiness_list']);
        $this->assign("bussiness_id",$result_data['bussiness_id']);
        $this->assign("bussiness_title",$result_data['bussiness_title']);
        $this->assign("bussiness_avatar",$result_data['bussiness_avatar']);
        $this->assign("recommend_bussiness",$result_data['recommend_bussiness']);
        $this->assign("recommend_connection",$result_data['recommend_connection']);
//var_dump($result_data['bussiness_list']);die;

        return $this->fetch();
    }

    /*我的人脉*/
    public function myConnection()
    {
        $data=input();
        $page=empty($data['page'])?1:input('page');
        $page_size=empty($data['page_size'])?10:input('page_size');

        $access_token=Session::get("access_token");
        $data1=array(
            "module"=>'Connection',
            "interface"=>'1007',
            "version"=>'1.0.0',
            "data"=> [
                "page_size"=> $page_size,
                "page"=> $page,
                "access_token"=> $access_token
            ]
        );
        $res=curl_post(config('self_url'),json_encode($data1));
        $result_code=json_decode($res,true)['result_code'];
        $result_info=json_decode($res,true)['result_info'];
        if(request()->isAjax())
        {
            if($result_code!=="0000")
            {
                return 0;
            }else {
                $result_data = json_decode($res, true)['result_data']['my_connection'];
                return json_encode($result_data);
            }
        }else{
            if($result_code!=="0000")
            {
                if($result_code=="1111")
                {
                    $this->redirect('Login/login');
                }
                return $this->error($result_info);

            }else{
                $result_info=json_decode($res,true)['result_data'];
                $this->assign("list",$result_info['my_connection']);
                return $this->fetch();
            }
        }

    }

    /*一键关注会员*/
    public function fastFollow()
    {
        $ids=json_decode(input("ids"));
        $access_token=Session::get("access_token");

        $data1=array(
            "module"=>'Connection',
            "interface"=>'1005',
            "version"=>'1.0.0',
            "data"=> [
                "follow_id"=> $ids,
                "type"=> 1,
                "access_token"=> $access_token
            ]
        );
        $res=curl_post(config('self_url'),json_encode($data1));
        $result_code=json_decode($res,true)['result_code'];
        $result_info=json_decode($res,true)['result_info'];
        return $res;

    }
    /*
     * 关注会员或商会
     * type  （1：会员2：商会）
     * */
    public function Follow()
    {
        $ids=json_decode(input("ids"));
        $type=input("type");

        $access_token=Session::get("access_token");

        $data1=array(
            "module"=>'Connection',
            "interface"=>'1005',
            "version"=>'1.0.0',
            "data"=> [
                "follow_id"=> $ids,
                "type"=> $type,
                "access_token"=> $access_token
            ]
        );
        $res=curl_post(config('self_url'),json_encode($data1));
        $result_code=json_decode($res,true)['result_code'];
        $result_info=json_decode($res,true)['result_info'];
        return $res;

    }
    /*发掘人脉*/
    public function Excavate()
    {
        $data=input();
        $access_token=Session::get("access_token");
        $data1=array(
            "module"=>'Connection',
            "interface"=>'1006',
            "version"=>'1.0.0',
            "data"=> [
                "access_token"=> $access_token
            ]
        );
        $res=curl_post(config('self_url'),json_encode($data1));
        $result_code=json_decode($res,true)['result_code'];
        $result_info=json_decode($res,true)['result_info'];

            if($result_code!=="0000")
            {
                if($result_code=="1111")
                {
                    $this->redirect('Login/login');
                }
                return $this->error($result_info);

            }else{
                $result_info=json_decode($res,true)['result_data'];
                $this->assign("list",$result_info['member_list']);
                return $this->fetch();
            }

    }

    /*组织结构*/
    public function departmentChart()
    {
        $department_id=input('id');
        if(empty($department_id))
        {
            $this->error("您还没有所属商会！");
        }
        /*获取本商会组织架构*/
        $department=Db::name('department')
            ->where('parent_id',0)
            ->where('bussiness_id',$department_id)
            ->field('id,title as name,parent_id as pid')
            ->find();

        $department['childrens']=[];
        $this->getChildrens($department,$department_id);
        $data['data'][0]=$department;
//        var_dump($data);die;
        if(request()->isPost())
        {
            return json_encode($data);
        }else{
            $this->assign('data',json_encode($data));
            return $this->fetch();
        }
    }
    /*添加子集*/
    public function getChildrens(&$department,$department_id)
    {
        $children=Db::name('department')
            ->where('parent_id',$department['id'])
            ->where('bussiness_id',$department_id)
            ->field('id,title as name,parent_id as pid')
            ->select();
        if(!empty($children))
        {


            foreach ($children as &$child)
            {
                $child['childrens']=[];
                $this->getChildrens($child,$department_id);
            }
            $department['childrens']=$children;
        }
    }
}